package com.snake.springboot.custom.service.impl.base;

import com.github.pagehelper.Page;
import com.snake.springboot.custom.api.model.base.CustCertModel;
import com.snake.springboot.custom.api.model.base.CustomModel;
import com.snake.springboot.custom.api.request.base.CustomAdminReq;
import com.snake.springboot.custom.api.request.base.CustomQuery;
import com.snake.springboot.custom.api.request.base.CustomReq;
import com.snake.springboot.custom.api.request.page.PageQuery;
import com.snake.springboot.custom.api.response.base.CustomRsp;
import com.snake.springboot.custom.api.service.base.*;
import com.snake.springboot.custom.dao.mybatis.entity.base.CustomDO;
import com.snake.springboot.custom.dao.mybatis.mappers.base.CustomMapper;
import com.snake.springboot.custom.service.assemble.base.CustomAssemble;
import com.snake.springboot.custom.service.port.adapter.event.AuthorizationDefaultCustomEvent;
import com.snake.springboot.custom.service.port.adapter.event.VerifyTextCodeEvent;
import com.snake.springboot.custom.service.utils.PageUtils;
import org.snake.common.result.ReturnList;
import org.snake.common.utils.ListUtils;
import org.snake.common.utils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商户信息实现类Service
 * 类{@code CustomServiceImpl} </br>
 * 创建时间 2025/02/08 09:36
 * @author yangxt
 * @see
 */
@Service
public class CustomServiceImpl implements CustomService{


    private CustomMapper customMapper;
    private CustMainAccountService accountService;
    private CustCertService custCertService;
    @Autowired
    private CustPointsService pointsService;
    @Autowired
    private ApplicationContext aCt;
    @Autowired
    private CustomAssService customAssService;
    @Autowired
    public CustomServiceImpl(CustomMapper customMapper, CustMainAccountService accountService, CustCertService custCertService) {
        this.customMapper = customMapper;
        this.accountService = accountService;
        this.custCertService = custCertService;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CustomReq addCustom(CustomReq customReq) {
        CustomDO customDO= CustomAssemble.customDOAss(customReq);
        customMapper.insert(customDO);
        accountService.addMainAccount(customReq);
        custCertService.addCert(customReq);
        pointsService.addPoints(customReq);
        return customReq;
    }

    @Override
    public void modifyCustom(CustomModel model) {
        CustomDO customDO= CustomAssemble.customDOAss(model);
        customMapper.updateCustom(customDO);
    }

    @Override
    public void adminModifyCustom(CustomAdminReq model) {
        CustomDO customDO= CustomAssemble.customDOAss(model);
        customMapper.updateCustom(customDO);
        custCertService.updateCert(model);
        accountService.updateAcc(model);
    }

    @Override
    public void removeCustom(String id) {
        customMapper.removeCustom(id);
    }

    @Override
    public ReturnList<CustomRsp> queryCustomList(PageQuery<CustomQuery> params) {
        Page<Object> objects = PageUtils.noForcePage(params.getPage());
        CustomDO customDO = CustomAssemble.customDOAss(params.getParams());
        List<CustomDO> dos =  customMapper.queryCustomList(customDO);
        List<CustomRsp> rspList = ListUtils.copy(dos, CustomRsp.class);
        this.customAssService.assertCustomList(rspList);
        return new ReturnList<>(rspList,objects.getTotal());
    }

    @Override
    public List<CustomRsp> queryCustomAllList(CustomQuery query) {
        CustomDO customDO = CustomAssemble.customDOAss(query);
        List<CustomDO> dos =  customMapper.queryCustomList(customDO);
        List<CustomRsp> rspList = ListUtils.copy(dos, CustomRsp.class);
        return rspList;
    }

    @Override
    public CustomRsp queryByCustomId(String id) {
        CustomDO customDO = customMapper.queryByCustomId(id);
        if (customDO==null){
            return null;
        }
        CustomRsp rsp =new CustomRsp();
        PropertyUtils.copyPropertiesIfNull(rsp,customDO);
        custCertService.queryDetail(rsp);
        this.customAssService.assertCustomList(ListUtils.initObjList(rsp));
        return rsp;
    }

    @Override
    public void modifyCert(CustCertModel model) {
        custCertService.modifyCert(model);
    }

    @Override
    public CustomReq register(CustomReq customReq) {
        //验证码校验
        //VerifyImageEvent event = new VerifyImageEvent(this, customReq.getVerifyImageModel());
        //文字验证码校验
        VerifyTextCodeEvent event = new VerifyTextCodeEvent(this, customReq.getVerifyTextCodeModel());
        aCt.publishEvent(event);
        CustomReq result = addCustom(customReq);
        AuthorizationDefaultCustomEvent adcEvent = new AuthorizationDefaultCustomEvent(customReq.getCustomId(),this);
        aCt.publishEvent(adcEvent);
        return result;
    }
}
